using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace stsdev {

    public enum IISVersionEnum {
        IIS6,
        IIS7orLater
    }

    public class TargetsBuilder {

        public static void Create(string FilePath) {
            FileStream stream = new FileStream(FilePath, FileMode.Create, FileAccess.Write);
            StreamWriter writer = new StreamWriter(stream);
            string TargetFileContents = Properties.Resources.Microsoft_SharePoint_targets;

            TargetFileContents = TargetFileContents.Replace("@ProjectName", SolutionBuilder.SolutionName);
            TargetFileContents = TargetFileContents.Replace("@DeploymentFilesFolder", Properties.Resources.DeploymentFilesFolder);
            TargetFileContents = TargetFileContents.Replace("@RootFilesFolder", Properties.Resources.RootFilesFolder);
            // Added 3/25/08 RHoward - Replacing @SystemDirectory token with path to the System directory
            TargetFileContents = TargetFileContents.Replace("@SystemDirectory", Environment.GetFolderPath(Environment.SpecialFolder.System));
            // End of 3/25/08 Add
            TargetFileContents = TargetFileContents.Replace("@StsDevPath", Assembly.GetExecutingAssembly().Location);

            TargetFileContents = TargetFileContents.Replace("@StsAdmDeployParameters", SolutionBuilder.StsAdmDeployParameters);
            TargetFileContents = TargetFileContents.Replace("@StsAdmUpgradeParameters", SolutionBuilder.StsAdmUpgradeParameters);
            TargetFileContents = TargetFileContents.Replace("@StsAdmRetractParameters", SolutionBuilder.StsAdmRetractParameters);

            // Added 4/3/2009 - bphillips - Allow user to select app pool.
            TargetFileContents = TargetFileContents.Replace("@TargetAppPool", SolutionBuilder.TargetAppPool);

            // Added 4/3/2009 - bphillips - Added support for IIS7 or later.
            if (SolutionBuilder.IISVersion == IISVersionEnum.IIS6) {
                TargetFileContents = TargetFileContents.Replace("@IISAppScript", "CSCRIPT.EXE C:\\WINDOWS\\system32\\iisapp.vbs /a \"$(TargetAppPool)\" /r");
            } else {
                TargetFileContents = TargetFileContents.Replace("@IISAppScript", "c:\\windows\\system32\\inetsrv\\APPCMD.EXE Recycle APPPOOL \"$(TargetAppPool)\"");
            }

            writer.Write(TargetFileContents);
            writer.Dispose();
            stream.Dispose();
        }
    }
}
